package templ

const Controller string = `
package controller

import (
	"ginfast/api/{{.Service}}/model"
	"ginfast/pkg/dao"
	"ginfast/pkg/gctx"
	"ginfast/util"

	"github.com/gin-gonic/gin"
)

// {{.Model}}
type {{.Model}} struct {
}

// ListHandler 列表
func ({{.Model}}) ListHandler(c *gin.Context) {
	var p dao.DbPage
	if err := c.ShouldBind(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	var data []model.{{.Model}}
	total, _ := p.DbModelWhere(&model.{{.Model}}{}).Find(&data)
	gctx.JSONWrite(gin.H{"total": total, "data": data}, c)
}

// GetHandler 获取指定id
func ({{.Model}}) GetHandler(c *gin.Context) {
	dao.QueryByID(&model.{{.Model}}, c)
}

// AddHandler 新增
func ({{.Model}}) AddHandler(c *gin.Context) {
	var p model.{{.Model}}
	//获取参数
	if err := c.ShouldBind(&p.{{.Model}}Opt); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	if err := dao.DbCreate(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	gctx.JSONOK(c)
}

// UpdateHandler 修改
func ({{.Model}}) UpdateHandler(c *gin.Context) {
	var p model.{{.Model}}
	//获取参数
	if err := c.ShouldBind(&p.{{.Model}}Opt); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	if err := dao.DbUpdateModel(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	gctx.JSONOK(c)
}

// DeleteHandler 删除
func ({{.Model}}) DeleteHandler(c *gin.Context) {
	dao.Deletes(&model.{{.Model}}, c)
}

func(o {{.Model}}) Router(r *gin.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.PUT("", o.UpdateHandler)
	r.DELETE("/:id", o.DeleteHandler)
}
`

const Model string = `
package model

// {{.Model}}Opt
type {{.Model}}Opt struct {
}

// {{.Model}}
type {{.Model}} struct {
	{{.Model}}Opt
}
`
